import annotation.Book;
import entity.BookStore;
import entity.User;
import org.junit.Test;
import service.UserService;
import service.imp.UserServiceImpl;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Properties;

public class Main {
    @Test
    public void test(){
        UserService userService = new UserServiceImpl();
        User login = userService.login("admin", "admin");
        System.out.println(login);
    }

    @Test
    public void test2(){

        Class<?> aClass = null;
        try {
            aClass = Class.forName("Main");
            System.out.println(aClass);
            Class<Main> mainClass = Main.class;
            System.out.println(mainClass);
            Class<? extends Main> aClass1 = new Main().getClass();
            System.out.println(aClass1);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test3(){
        try {
            Class<?> aClass = Class.forName("entity.User");
            Field[] fields = aClass.getFields();
            for (Field field : fields) {
                System.out.println(field.getName());
            }

//            Field username = aClass.getField("username");
//            System.out.println(username.getName());

            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field field : declaredFields) {
                System.out.println(field.getName());
            }
            Field password = aClass.getDeclaredField("password");
            System.out.println(password.getName());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test4(){
        try {
            // 加载配置文件
            InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("pro.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            // className  methodName
            String className = properties.getProperty("className");
            String methodName = properties.getProperty("methodName");
            Class<?> aClass = Class.forName(className);
            Method method = aClass.getMethod(methodName);
            Object o = aClass.newInstance();
            // 调用方法
            method.invoke(o);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test5(){
        // 获取类上面的注解
        Class<BookStore> bookStoreClass = BookStore.class;
        // 判断类上是否有注解
        if (bookStoreClass.isAnnotationPresent(Book.class)) {
            Book annotation = bookStoreClass.getAnnotation(Book.class);
            System.out.println(annotation.value());
            System.out.println(annotation.price());
            System.out.println(Arrays.toString(annotation.authors()));
        }
    }
    @Test
    public void test6() throws NoSuchMethodException {
        // 获取方法上面的注解
        Class<BookStore> bookStoreClass = BookStore.class;
        // 判断方法上是否有注解
        Method buyBook = bookStoreClass.getDeclaredMethod("buyBook");
        if (buyBook.isAnnotationPresent(Book.class)) {
            Book annotation = buyBook.getAnnotation(Book.class);
            System.out.println(annotation.value());
            System.out.println(annotation.price());
            System.out.println(Arrays.toString(annotation.authors()));
        }
    }
}